package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC464 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 未来需要再次复习此道题目
     * 看完别人的代码，就自知效率甚低。
     * 此种位运算的方式，已经达到了Java效率的巅峰
     * @return:
     */
    class Solution {
        public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
            int canReachTotal = (1 + maxChoosableInteger) * maxChoosableInteger / 2;
            if (canReachTotal < desiredTotal) { // 达不到
                return false;
            } else if (canReachTotal == desiredTotal) { // 刚好达到，maxChoosableInteger奇数赢
                return (maxChoosableInteger & 1) == 1;
            }

            return canWin(0, desiredTotal, maxChoosableInteger, new int[1 << maxChoosableInteger]);
        }

        private boolean canWin(int bits, int distance, int maxChoosableInteger, int[] dp) {
            if (dp[bits] != 0) { // 已经计算过。0：未计算，1：true，2：false
                return dp[bits] == 1;
            }

            boolean result = false;
            for (int cur = maxChoosableInteger; cur > 0; cur--) {
                int curBit = 1 << (cur - 1);
                if ((bits & curBit) == 0) { // 当前值没有被使用
                    if (cur >= distance // 可以一步成功
                            || !canWin(bits | curBit, distance - cur, maxChoosableInteger, dp)) { // 如果能找到一步让对方无法赢
                        result = true;
                        break;
                    }
                }
            }

            dp[bits] = result ? 1 : 2;
            return result;
        }
    }

    class Solution_Low_Effeficiency {
        Map<String, String> strMap = new HashMap<>();

        public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
            if (desiredTotal <= maxChoosableInteger) return true;
            if (desiredTotal * 1.0 > 1.0 * maxChoosableInteger * (maxChoosableInteger + 1) / 2) return false;
            boolean[] visited = new boolean[maxChoosableInteger + 1];

            return false;
        }

        public boolean dfs(int maxChoosableInteger, int desiredTotal, boolean[] visited) {
            return false;
        }
    }

    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC464 an = new LC464();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
